#include "parameterquery.h"
#include "ui_parameterquery.h"

#include <QDateTime>

#include "common/data.h"
#include "common/tool.h"

ParameterQuery::ParameterQuery(QWidget* parent)
    : QWidget(parent)
    , ui(new Ui::ParameterQuery)
{
    ui->setupUi(this);

    connect(ui->comboThat, &QComboBox::currentTextChanged, this, [&]()
            {
        showTable(ui->tableThat, ui->comboThat->currentData().toInt());
        // 基本信息
        showCompare(ui->tableThis, ui->tableThat, ui->tableThat->rowCount() - 2);
        // 特征点
        showCompare(static_cast<QTableWidget *>(ui->tableThis->cellWidget(ui->tableThis->rowCount() - 2, 0)),
            static_cast<QTableWidget *>(ui->tableThat->cellWidget(ui->tableThat->rowCount() - 2, 0)),
            static_cast<QTableWidget *>(ui->tableThat->cellWidget(ui->tableThat->rowCount() - 2, 0))->rowCount());
        // 任务段
        showCompare(static_cast<QTableWidget *>(ui->tableThis->cellWidget(ui->tableThis->rowCount() - 1, 0)),
            static_cast<QTableWidget *>(ui->tableThat->cellWidget(ui->tableThat->rowCount() - 1, 0)),
            static_cast<QTableWidget *>(ui->tableThat->cellWidget(ui->tableThat->rowCount() - 1, 0))->rowCount()); });
}

ParameterQuery::~ParameterQuery()
{
    delete ui;
}

void ParameterQuery::display(int idxThis) const
{
    ui->comboThis->setPlaceholderText(Data::missions[idxThis].code);
    ui->comboThat->clear();

    ui->tableThat->clearContents();
    ui->tableThat->setRowCount(0);

    showTable(ui->tableThis, idxThis);

    for (int i = 0; i < Data::missions.size(); ++i)
    {
        if ((i != idxThis) && (Data::missions[i].rocketModel == Data::missions[idxThis].rocketModel) && (ui->comboThat->findText(Data::missions[i].code) == -1))
        {
            ui->comboThat->addItem(Data::missions[i].code, i);
        }
    }
}

void ParameterQuery::showTable(QTableWidget* table, int index) const
{
    table->clearContents();
    table->setRowCount(0);
    table->horizontalHeader()->hide();

    Tool::setTableHeaders(table, {"参数值"});
    Tool::appendTableRow(table, {Data::missions[index].brief});
    Tool::appendTableRow(table, {Data::missions[index].mark});
    Tool::appendTableRow(table, {Data::missions[index].rocketModel});
    Tool::appendTableRow(table, {Data::missions[index].coordinate});
    Tool::appendTableRow(table, {Data::missions[index].method});
    Tool::appendTableRow(table,
                         {QString("%1 (经度：%2, 纬度：%3, 高程：%4)")
                              .arg(Data::missions[index].launchPoint)
                              .arg(Data::missions[index].launchLongitude)
                              .arg(Data::missions[index].launchLatitude)
                              .arg(Data::missions[index].launchAltitude)});
    Tool::appendTableRow(table, {QString::number(Data::missions[index].launchAzimuth)});
    Tool::appendTableRow(table, {Data::missions[index].launchTime.toString(Tool::DATETIME_FORMAT)});

    // 特征点
    QTableWidget* tableUniquePoint = new QTableWidget;
    Tool::setTableHeaders(tableUniquePoint, {"参数名称", "装订值"});
    tableUniquePoint->verticalHeader()->hide();
    for (const auto& t : Data::missions[index].uniquePoint)
    {
        Tool::appendTableRow(tableUniquePoint, {std::get<0>(t), QString::number(std::get<1>(t))});
    }
    int row = table->rowCount();
    table->insertRow(row);
    table->setCellWidget(row, 0, tableUniquePoint);
    table->verticalHeader()->setSectionResizeMode(row, QHeaderView::ResizeToContents);

    // 任务段
    QTableWidget* tableTaskSegment = new QTableWidget;
    Tool::setTableHeaders(tableTaskSegment, {"站点", "设备", "开始时间", "结束时间"});
    tableTaskSegment->verticalHeader()->hide();
    for (const auto& t : Data::missions[index].taskSegment)
    {
        Tool::appendTableRow(tableTaskSegment, {std::get<0>(t), std::get<1>(t), QString::number(std::get<2>(t)), QString::number(std::get<3>(t))});
    }
    row = table->rowCount();
    table->insertRow(row);
    table->setCellWidget(row, 0, tableTaskSegment);
    table->verticalHeader()->setSectionResizeMode(row, QHeaderView::ResizeToContents);

    table->setVerticalHeaderLabels({"任务简介", "任务标志", "火箭型号", "坐标系", "通信方式", "发射点参数", "发射方位角", "发射时间", "特征点", "任务段"});
    table->verticalHeader()->setSectionResizeMode(0, QHeaderView::ResizeToContents);
}

void ParameterQuery::showCompare(QTableWidget* thisTable, QTableWidget* thatTable, int rowCnt) const
{
    int colCnt = thatTable->columnCount();

    for (int row = 0; row < rowCnt; ++row)
    {

        if (row == thisTable->rowCount())
        {
            for (int r = row; r < rowCnt; ++r)
            {
                for (int c = 0; c < colCnt; ++c)
                {
                    thatTable->item(r, c)->setForeground(QColor(Qt::red));
                }
            }
            break;
        }

        QString thisInfo;
        for (int col = 0; col < colCnt; ++col)
        {
            thisInfo += thisTable->item(row, col)->text();
        }
        QString thatInfo;
        for (int col = 0; col < colCnt; ++col)
        {
            thatInfo += thatTable->item(row, col)->text();
        }
        if (thisInfo != thatInfo)
        {
            for (int col = 0; col < colCnt; ++col)
            {
                thatTable->item(row, col)->setForeground(QColor(Qt::red));
            }
        }
    }
}
